package org.jeecg.modules.api.wlhy.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.EntityDict;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.entity.HyOrderApiDTO;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.order.vo.HyOrderVO;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
* @Description: hy_order
* @Author: jeecg-boot
* @Date:   2020-12-03
* @Version: V1.0
*/
@Api(tags="hy_order")
@RestController
@RequestMapping("/api/wlhy/order")
@Slf4j
public class ApiOrderController extends JeecgController<HyOrder, IHyOrderService> {
   @Autowired
   private IHyOrderService hyOrderService;

    @Autowired
    private IHyCustomerService hyCustomerService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyDriverService hyDriverService;

   @ApiOperation(value="hy_order-货源大厅货源分页列表查询", notes="hy_order-货源大厅货源分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(HyOrder hyOrder,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {

       QueryWrapper<HyOrder> queryWrapper = QueryGenerator.initQueryWrapper(hyOrder, req.getParameterMap());
       queryWrapper.eq("order_status","2");//发布状态
       queryWrapper.eq("order_type","2"); //货源类型(1指派2发布到货源大厅)
       Page<HyOrder> page = new Page<HyOrder>(pageNo, pageSize);
       IPage<HyOrder> pageList = hyOrderService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

 /*   @ApiOperation(value="hy_order-根据位置查询货源分页列表", notes="根据位置查询货源分页列表")
    @GetMapping(value = "/publist")
    public Result<?> publist(HyOrderVO hyOrdervo,
                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                             HttpServletRequest req) {

        QueryWrapper<HyOrderVO> queryWrapper = QueryGenerator.initQueryWrapper(hyOrdervo, req.getParameterMap());
        queryWrapper.eq("order_status","2");//发布状态
        queryWrapper.eq("order_type","2"); //货源类型(1指派2发布到货源大厅)
        Page<HyOrderVO> page = new Page<HyOrderVO>(pageNo, pageSize);
        IPage<HyOrderVO> pageList = hyOrderService.getPageList(page, queryWrapper);
        return Result.OK(pageList);
    }*/

    @ApiOperation(value="hy_order-根据位置查询货源分页列表", notes="根据位置查询货源分页列表")
    @GetMapping(value = "/publist")
    public Result<?> publist(
                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                             @RequestParam(name="lng") BigDecimal lng,
                             @RequestParam(name="lat") BigDecimal lat,
                             @RequestParam(name="dis", defaultValue="0") Integer dis,
                             @RequestParam(name="keyword") String keyword,
                             HttpServletRequest req) {
        /*if(lng==null || lat==null){
            return Result.error("位置信息不能为空");
        }*/

        if(dis < 0){
            return Result.error("距离信息错误");
        }

        Map p = new HashMap();
        p.put("lng",lng);
        p.put("lat",lat);

        if(dis !=null && dis!=0) {
            p.put("dis", dis);
        }

        if(StringUtils.isNotEmpty(keyword)){
            p.put("keyword", keyword);
        }

        Page<HyOrderVO> page = new Page<HyOrderVO>(pageNo, pageSize);
        IPage<HyOrderVO> pageList = hyOrderService.getPageListByDis(page, p);
        return Result.OK(pageList);
    }

    @ApiOperation(value="货源分页列表关键词查询", notes="货源分页列表关键词查询")
    @GetMapping(value = "/listbykeyword")
    public Result<?> queryPageListByKeyword(
            @RequestParam(name="keyword") String keyword,
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req) {
        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);

        QueryWrapper<HyOrder> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("customer_id",hyCustomer.getId());
        queryWrapper.eq("order_status","2");//发布状态
        queryWrapper.and(
                qw -> qw.like("send_address_name", keyword)
                        .or().like("receive_address_name", keyword)
        );

        queryWrapper.orderByDesc("create_time");

        Page<HyOrder> page = new Page<>(pageNo, pageSize);
        IPage<HyOrder> pageList = hyOrderService.page(page, queryWrapper);
        if(pageList.getTotal() == 0){
            return Result.error("暂无数据");
        }

        return Result.OK(pageList);
    }


   @ApiOperation(value="hy_order-通过id查询", notes="hy_order-通过id查询")
   @GetMapping(value = "/queryById")
   @EntityDict
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {

       if (StringUtils.isEmpty(id)) {
           return Result.error("未找到货源信息");
       }

       HyOrder order = hyOrderService.getById(id);
       if(order==null) {
           return Result.error("未找到货源信息");
       }
       //检查货源信息
       if ("8".equals(order.getOrderStatus())) {
           return Result.error("该货源已完结");
       }
       if (!"2".equals(order.getOrderStatus())) {
           return Result.error("该货源未发布");
       }



       return Result.OK(order);
   }

    @ApiOperation(value="根据货源号查询货源", notes="根据货源号查询货源")
    @GetMapping(value = "/queryByOrderNum")
    public Result<?> queryByOrderNum(@RequestParam(name="orderNum",required=true) String orderNum,
                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
                                     ) {
        QueryWrapper<HyOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status","2");//发布状态
        queryWrapper.eq("order_type","1"); //货源类型(1指派2发布到货源大厅)
        queryWrapper.likeLeft("order_num", orderNum);
        Page<HyOrder> page = new Page<HyOrder>(pageNo, pageSize);
        IPage<HyOrder> pageList = hyOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value="hy_order-货源大厅公开货源列表查询", notes="hy_order-货源大厅货源分页列表查询")
    @GetMapping(value = "/newList")
    public Result<?> queryNewPageList(@RequestParam(name="keyword") String keyword,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="100") Integer pageSize,
                                   HttpServletRequest req) {

        QueryWrapper<HyOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status","2");//发布状态
        queryWrapper.eq("order_type","2"); //货源类型(1指派2发布到货源大厅)

        /*if (StringUtils.isNotEmpty(hyOrder.getSendAddressName())){
            queryWrapper.like("send_address_name", hyOrder.getSendAddressName());
        }
        if (StringUtils.isNotEmpty(hyOrder.getReceiveAddressName())){
            queryWrapper.like("receive_address_name", hyOrder.getReceiveAddressName());
        }*/
        queryWrapper.and(
                qw -> qw.like("send_address_name", keyword)
                        .or().like("receive_address_name", keyword)
        );
        Page<HyOrder> page = new Page<HyOrder>(pageNo, pageSize);
        IPage<HyOrder> pageList = hyOrderService.page(page, queryWrapper);
        for (HyOrder order:pageList.getRecords()){
            order.setSendContactTel(DataMaskingUtil.phoneMasking(order.getSendContactTel()));
            order.setSendContactCredit(DataMaskingUtil.idcardMasking(order.getSendContactCredit()));
            order.setReceiveContactTel(DataMaskingUtil.phoneMasking(order.getReceiveContactTel()));
            order.setReceiveContactCredit(DataMaskingUtil.idcardMasking(order.getReceiveContactCredit()));
        }
        return Result.OK(pageList);
    }
    @ApiOperation(value="hy_order-货源大厅公开货源获取详情", notes="hy_order-货源大厅获取详情")
    @GetMapping(value = "/getInfo")
    @EntityDict
    public Result<?> getInfo(@RequestParam(name="id",required=true) String id){
        if (StringUtils.isEmpty(id)) {
            return Result.error("未找到货源信息");
        }

        HyOrderApiDTO orderApiDTO = hyOrderService.getInfoById(id);
        if(orderApiDTO==null) {
            return Result.error("未找到货源信息");
        }
        //检查货源信息
        if ("8".equals(orderApiDTO.getOrderStatus())) {
            return Result.error("该货源已完结");
        }
        if (!"2".equals(orderApiDTO.getOrderStatus())) {
            return Result.error("该货源未发布");
        }
        return Result.OK(orderApiDTO);
    }


}
